package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.hpin.mlxteacher.photoview.PhotoView;
import com.hpin.mlxteacher.widget.PreviewPhotoActivity;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Aaron on 15/8/18.
 */
public class ProviewAdapter extends PagerAdapter {

    private List<PhotoView> photoviews;
    private Context ctx;

    public ProviewAdapter(List<PhotoView> photoviews, Context ctx) {
        this.ctx = ctx;
        this.photoviews = photoviews;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView(photoviews.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        PhotoView photoView = photoviews.get(position);

        container.addView(photoView);

        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

//                LogUtil.e("TAG", "photoview 条目点击事件");

                PreviewPhotoActivity preview = (PreviewPhotoActivity) ctx;
                preview.finish();

            }
        });

        photoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                //长按保存
//                LogUtil.e("tag", "长按事件");
                EventBus.getDefault().post(position);

                return false;
            }
        });

        return photoView;
    }

    @Override
    public int getCount() {
        return photoviews.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }
}
